package com.google.code.ptrends.Application.DAL;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.google.code.ptrends.Application.DAL.entities.CategoryEntity;

@Stateless
@LocalBean
public class CategoryServiceBean {

	@PersistenceContext(name = "Application.DAL")
	private EntityManager em;

	public CategoryServiceBean() {
	}

	// do we need this method?
	public void createCategory(final String name, final String description) {
		// if (StringUtils.isEmpty(name)) {
		// throw new IllegalArgumentException("Illegal null CategoryEntity name");
		// }
		CategoryEntity categoryEntity = new CategoryEntity();
		categoryEntity.setName(name);
		categoryEntity.setDescription(description);
		em.persist(categoryEntity);
	}

	// do we need this method?
	public void removeCategory(final int id) {
		CategoryEntity categoryEntity = findCategory(id);

		if (categoryEntity != null) {
			em.remove(categoryEntity);
		}
	}

	public CategoryEntity findCategory(final int id) {
		return em.find(CategoryEntity.class, id);
	}

	public CategoryEntity findCategory(final String name) {
		return em.find(CategoryEntity.class, name);
	}

	public List<CategoryEntity> findAllCategories() {
		TypedQuery<CategoryEntity> query = em.createQuery(
				"SELECT c FROM CategoryEntity c", CategoryEntity.class);
		return query.getResultList();
	}
}